package com.csu.mypetstore.api.controller.front;

import com.csu.mypetstore.api.common.CommonResponse;
import com.csu.mypetstore.api.common.ResponseCode;
import com.csu.mypetstore.api.entity.Category;
import com.csu.mypetstore.api.entity.Item;
import com.csu.mypetstore.api.entity.Product;
import com.csu.mypetstore.api.service.CatalogService;
import com.csu.mypetstore.api.vo.AccountVO;
import com.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/catalog/")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @GetMapping("categories")
    @ResponseBody
    public CommonResponse<List<Category>> getCategoryList() {
        return catalogService.getCategoryList();
    }

    @GetMapping("categories/{id}")
    @ResponseBody
    public CommonResponse<Category> getCategory(@PathVariable("id") String categoryId) {
        return catalogService.getCategory(categoryId);
    }

    @GetMapping("categories/{id}/products")
    @ResponseBody
    public CommonResponse<List<Product>> getProductListByCategoryId(@PathVariable("id") String categoryId) {
        return catalogService.getProductListByCategoryId(categoryId);
    }

    @GetMapping("products/{id}")
    @ResponseBody
    public CommonResponse<Product> getProductById(@PathVariable("id") String productId) {
        return catalogService.getProductById(productId);
    }

    @GetMapping("products/{id}/items")
    @ResponseBody
    public CommonResponse<List<ItemVO>> getItemsByProductId(@PathVariable("id") String productId) {
        return catalogService.getItemsByProductId(productId);
    }

    //通过自身id 查看Item  已确认可行
    @GetMapping("items/{id}")
    @ResponseBody
    public CommonResponse<ItemVO> getItemByItemId(@PathVariable("id") String itemId) {
        return catalogService.getItemByItemId(itemId);
    }


    @GetMapping("getProductListByKeyword/{keyword}")
//    @PostMapping("searchProducts")
    @ResponseBody
    public CommonResponse<List<Product>> getProductListByKeyword(@PathVariable("keyword") String keyword) {
        return catalogService.getProductListByKeyword("%" + keyword.toLowerCase() + "%");
    }

//    @PostMapping("getProductListByKeyword")
////    @PostMapping("searchProducts")
//    @ResponseBody
//    public CommonResponse<List<Product>> getProductListByKeyword(@RequestParam("keyword") String keyword) {
//        return catalogService.getProductListByKeyword("%" + keyword.toLowerCase() + "%");
//    }

}
